<template>
  <el-dialog :title="form.id?'修改职员':'新增职员'" :visible.sync="show" width="70%" v-if="show">
    <span class="wf" v-if="show">
      <el-form ref="form" :model="form" label-width="80px">
        <div class="wf x">
          <el-form-item label="id">
            <el-input v-model="form.id" disabled />
          </el-form-item>
          <el-form-item label="隶属部门">
            <!-- <el-input v-model="form.pid" /> -->
            <el-cascader
              :show-all-levels="false"
              :options="userType"
              :props="{ value:'id',label:'name',checkStrictly: true}"
              v-model="itemPid"
              clearable
            />
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="form.name" />
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="form.tel" />
          </el-form-item>
        </div>
        <div class="wf x">
          <el-form-item label="用户类型">
            <!-- <el-input v-model="form.pid" /> -->
            <el-cascader
              :show-all-levels="false"
              :options="types"
              :props="{ value:'id',label:'name',checkStrictly: true}"
              v-model="typePid"
              clearable
            />
          </el-form-item>
          <el-form-item label="用户名">
            <el-input v-model="form.username" :disabled="form.id!=0" />
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="form.password" />
          </el-form-item>
        </div>

        <el-form-item label="备注">
          <el-input v-model="form.other.remarks"></el-input>
        </el-form-item>
      </el-form>
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false; ">取 消</el-button>
      <el-button type="primary" @click="show = false; send(form)">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["fshow", "form"],
  watch: {
    fshow(a) {
      this.show = true;
    },
    form(a) {
      this.form_(a);
    },
    "$gd.userType"(a) {
      this.$gd_(a);
    },
    itemPid(a) {
      if (a.length) {
        this.form.userType = a[a.length - 1];
      } else {
        this.form.userType = 0;
      }
    },
    typePid(a) {
      if (a.length) {
        this.form.type = a[a.length - 1];
      } else {
        this.form.type = 0;
      }
      // console.log(this.$cp(a));
    }
  },
  data() {
    return {
      show: false,
      fetch: [],
      defUnit: { name: "", ratio: 1, remarks: "" },
      userType: [],
      itemPid: [],
      typePid: [],
      types: []
    };
  },
  created() {
    this.gd_(this.$gd);
    this.form_(this.form);
  },
  methods: {
    createFilter(queryString) {
      return restaurant => {
        return (
          restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    querySearch(queryString, cb) {
      let restaurants = this.fetch;
      let results = queryString
        ? restaurants.filter(this.createFilter(queryString))
        : restaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    send(v) {
      const data = {
        cmd: v.id ? 1 : 2,
        val: {
          token: sessionStorage.token,
          table: "users"
        }
      };
      let tmp = this.$del(v, "id");
      if (v.id) {
        tmp.where = { id: v.id };
      }

      data.val = Object.assign(data.val, tmp);
      this.$post(data, res => {
        // console.log(this.$cp(res),data);
        if (res.result) {
          this.$emit("change", 201,res.result,!v.id);
        }
      });
    },
    gd_() {
      this.userType = this.$gd.userType.arr2tree();
      this.types = this.$gd.userType2;
    },
    form_(a) {
      if (!a) a = { name: "", other: { remarks: "" } };
      if (!a.other) a.other = { remarks: "" };
      this.itemPid = this.$gd.userType.getTreeID(a.userType);
      if (a.type) {
        this.typePid = [a.type];
      }
    }
  }
};
</script>

